Strict
Import agfx_image
Import agfx_extension

Class TImageContainer Extends StringMap<TImage>
	
	Field path$ = "graphics/"
	
	Method Load:TImage(name:String, nameoverride:String = "", midhandle:Bool=true, ignoreCache:Bool=False)
		' check if we already have the image in the bank!
		Local storeKey:String = nameoverride.ToUpper()
		If storeKey = "" Then storeKey = StripAll(name.ToUpper())
		If Not ignoreCache And Self.Contains(storeKey) Then Return Self.Get(storeKey)
		
		' discard the old image if it's there
		If Self.Contains(storeKey) Then Self.Get(storeKey).image.Discard()
		
		Local i:TImage = New TImage
		i.Load(path + name, midhandle)
		i.name = storeKey
		Self.Set(i.name, i)
		Return i
	End

	
	Method LoadTileset:TImage(name:String, tileWidth%, tileHeight%, tileMargin% = 0, tileSpacing% = 0, nameoverride:String = "", midhandle:Bool=False, ignoreCache:Bool=False)
		' check if we already have the image in the bank!
		Local storeKey:String = nameoverride.ToUpper()
		If storeKey = "" Then storeKey = StripAll(name.ToUpper())
		If Not ignoreCache And Self.Contains(storeKey) Then Return Self.Get(storeKey)
		
		' discard the old image if it's there
		If Self.Contains(storeKey) Then Self.Get(storeKey).image.Discard()
		
		' load the new one
		Local i:TImage = New TImage()
		i.LoadTileset(path + name, tileWidth, tileHeight, tileMargin, tileSpacing, midhandle)
		i.name = storeKey
		Self.Set(i.name, i)
		Return i
	End


	Method LoadAnim:Void(name:String, w:Int, h:Int, total:Int, tmpImage:Image, midhandle:Bool=True)
		Local i:TImage = New TImage()
		i.LoadAnim(path + name, w, h, total, tmpImage, midhandle)
		Self.Set(i.name, i)
	End
   
	Method Find:TImage(name:String)
		name = name.ToUpper()

		Local i:TImage =  Self.Get(name)
		AssertNotNull(i, "Image '" + name + "' not found in the ImageBank")
		Return i
	End
	
	Method PreCache:Void()
		Local gi:TImage
		Local key:String
		For key = Eachin Self.Keys()
			gi = Self.Get(key)
			gi.PreCache()
		Next
	End
	
	Method Dump:Void()
		' debug: print all keys in the map
		Local key:String
		For key = EachIn self.Keys()
			Print key + " is stored in the map."
		Next
	End
	
End



